package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.convert.TeachplanMediaConvert;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 * 课程计划控制层
 * </p>
 */
@RestController
public class TeachplanController implements TeachplanApi {
    @Autowired
    private TeachplanService teachplanService;


    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseId) {
        //1. 获得公司Id
        Long companyId = SecurityUtil.getCompanyId();
        //2. 调用service获得 dto数据
        TeachplanDTO teachplanDTO = 
            teachplanService.queryTreeNodesByCourseId(courseId, companyId);
        return teachplanDTO;
    }

    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO teachplanVO) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO dto = TeachplanConvert.INSTANCE.vo2dto(teachplanVO);
        TeachplanDTO result = teachplanService.createOrModifyTeachPlan(dto, companyId);
        return result;
    }

    @DeleteMapping("teachplan/{teachPlanId}")
    public void removeTeachPlan(@PathVariable Long teachPlanId) {
        Long companyId = SecurityUtil.getCompanyId();
        teachplanService.removeTeacherPlan(teachPlanId, companyId);
    }

    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanMediaDTO dto = new TeachplanMediaDTO();
        dto.setTeachplanId(vo.getTeachplanId());
        dto.setMediaId(vo.getMediaId());
        TeachplanMediaDTO teachplanMediaDTO = teachplanService.associateMedia(dto, companyId);
        return teachplanMediaDTO;
    }
}